<template>
  <div class="login_container" v-if="flag">
    <h1 class="title">物流管理系统</h1>
    <el-form
      label-position="right"
      label-width="100px"
      :rules="loginRules"
      ref="loginForm"
      :model="loginFormData"
      class="login_register_Form"
    >
      <el-form-item label="登录名" prop="username">
        <el-input
          v-model="loginFormData.username"
          prefix-icon="el-icon-user"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="loginFormData.password"
          type="password"
          prefix-icon="el-icon-search"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loginForm('loginForm')"
          >立即登录</el-button
        >
        <el-button @click="resetForm('loginForm')">重置</el-button>
        <el-link type="primary" class="toRegister" @click="flag = false"
          >去注册</el-link
        >
      </el-form-item>
    </el-form>
  </div>
  <div class="register_container" v-else>
    <h1 class="title">物流管理系统</h1>
    <el-form
      label-position="right"
      label-width="100px"
      :rules="registerRules"
      ref="registerForm"
      :model="registerFormData"
      class="login_register_Form"
    >
      <el-form-item label="登录名" prop="username">
        <el-input
          v-model="registerFormData.username"
          prefix-icon="el-icon-user"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="registerFormData.password"
          type="password"
          prefix-icon="el-icon-search"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="repassword">
        <el-input
          v-model="registerFormData.repassword"
          type="password"
          prefix-icon="el-icon-search"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="registerFormData.phone"
          type="number"
          prefix-icon="el-icon-phone"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="registerForm('registerForm')"
          >立即注册</el-button
        >
        <el-button @click="resetForm('registerForm')">重置</el-button>
        <el-link type="primary" class="toLogin" @click="flag = true"
          >去登录</el-link
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { postLogin, postRegister } from '../tools/Ajax.js'

export default {
  name: 'MyLogin',
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.registerFormData.repassword !== '') {
          this.$refs.registerForm.validateField('repassword')
        }
        callback()
      }
    }
    var validateRepassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registerFormData.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    var checkphone = (rule, value, callback) => {
      // let phoneReg = /(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/;
      if (value == '') {
        callback(new Error('请输入手机号'))
      } else if (!this.isCellPhone(value)) {
        //引入methods中封装的检查手机格式的方法
        callback(new Error('请输入正确的手机号!'))
      } else {
        callback()
      }
    }
    return {
      flag: true,
      loginFormData: {
        username: '',
        password: '',
      },
      registerFormData: {
        username: '',
        password: '',
        repassword: '',
        phone: '',
      },
      loginRules: {
        username: [
          { required: true, message: '请输入登录名', trigger: 'change' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'change',
          },
        ],
      },
      registerRules: {
        username: [
          { required: true, message: '请输入登录名', trigger: 'change' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'change',
          },
        ],
        password: [
          { required: true, validator: validatePass, trigger: 'change' },
        ],
        repassword: [
          { required: true, validator: validateRepassword, trigger: 'change' },
        ],
        phone: [{ required: true, validator: checkphone, trigger: 'change' }],
      },
    }
  },
  methods: {
    loginForm(formName) {
      this.$refs[formName].validate((valid) => {
        // 校验失败
        if (!valid) return console.log('登陆失败!!')
        // 成功
        // postLogin(this.loginFormData).then((res) => {
        //   console.log(res)
        // })
        this.$http.post('/myApi/api/login', this.loginFormData).then((res) => {
          console.log(res.data)
          if (res.data.code !== 200) {
            this.$message.warning('登录失败，用户名或密码错误')
          } else {
            this.$message.success('登录成功')
            window.sessionStorage.setItem('token', res.data.token)
            this.$store.commit('setUserInfo', res.data.data)
            this.$router.push('/home')
          }
        })
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      if (formName == 'loginForm') {
        this.loginFormData = {
          username: '',
          password: '',
        }
      }
      if (formName == 'registerForm') {
        this.registerFormData = {
          username: '',
          password: '',
          repassword: '',
          phone: '',
        }
      }
    },
    registerForm(formName) {
      this.$refs[formName].validate((valid) => {
        // 校验失败
        if (!valid) return console.log('注册失败!!')

        this.$http
          .post('myApi/api/register', this.registerFormData)
          .then((res) => {
            console.log(res.data)
            if (res.data.code !== 200) {
              this.$message.warning('注册失败，用户名或密码错误')
            } else {
              this.$message.success('注册成功')
              // window.sessionStorage.setItem('token', res.data.token)
              // 显示登录表单
              this.flag = true
            }
          })
      })
    },
    //检查手机号
    isCellPhone(val) {
      if (!/^1(3|4|5|6|7|8)\d{9}$/.test(val)) {
        return false
      } else {
        return true
      }
    },
  },
}
</script>

<style lang="less" scoped>
.login_container,
.register_container {
  position: relative;
  width: 100%;
  height: 100%;
  background-image: url('../assets/images/wuliu.jpg');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  opacity: 0.8;
  // background-color: #22a7f2;
}
.title {
  // font-size: 28px;
  padding-left: 50px;
  padding-top: 50px;
}
.login_register_Form {
  position: absolute;
  top: 200px;
  right: 100px;
  width: 600px;
  height: 400px;
  border-radius: 10px;
  // margin: 100px auto;
  padding: 50px;
  font-size: 24px !important;
  color: blue;
  background-color: #fff;
  // background-color: #2b4b6b;
}
.toRegister,
.toLogin {
  position: absolute;
  right: 0px;
  bottom: 0px;
  font-size: 20px;
}

// .login_container {
//   height: 100%;
//   background-color: #2b4b6b;
// }
</style>
